//
//  LTQHomeViewController.swift
//  WeiBoProject
//
//  Created by MAC on 16/2/24.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
import SVProgressHUD
//    定义重用标示
let LTQHomeViewControllerReuserIdenfiter = "LTQHomeViewControllerReuserIdenfiter"
class LTQHomeViewController: LTQBaseTableViewController {


    var statuslistViewModel: LTQStatusListViewModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        vistor?.setupInfo("关注一些人，回这里看看有什么惊喜", image: "visitordiscover_feed_image_house", animation: true)
        if isLogin{
            
            statuslistViewModel = LTQStatusListViewModel()
//            判断登陆后，首次加载数据
            loadData()
            
            tableView.registerClass(LTQHomeTableViewCell.self, forCellReuseIdentifier: LTQHomeViewControllerReuserIdenfiter)
            
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 85
//            上提加载更多
            tableView.tableFooterView = pullView
//            下拉加载更多
            tableView.addSubview(refreshView)
            
            refreshView.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
            
        }
        
        
        
     
    }

    
    
    func loadData(){
        
        SVProgressHUD.show()
        
//       最新的数据 微博上面  
//        下拉更多，去改变 since_id ,max_id = 0
//      上提更多,需要把 since_id = 0
        var since_id = statuslistViewModel?.array.first?.statusModel?.id ?? 0
        
//       旧的数据  微博下面
        var max_id = 0
        
        if pullView.isAnimating(){
            
            since_id = 0
            
            max_id = statuslistViewModel?.array.last?.statusModel?.id ?? 0
            //拿到最后一条数据
            max_id -= 1
            
        }
        
        
        statuslistViewModel?.loadData(since_id, max_id: max_id, finish: { (count:Int) -> () in
            
                        //菊花转的时候,肯定是上体更多
            if !self.pullView.isAnimating() {
                //显示刷新提示
                self.showNewCount(count)
            }
            
            self.tableView.reloadData()
          
            self.pullView.stopAnimating()
            
            SVProgressHUD.dismiss()
            
//            下拉刷新时，要让view能够回到Nav后面的位置
            self.refreshView.endRefreshing()
            
        })
        
    }
/*
    1.数据显示问题
*/

//MARK:-- 显示刷新提示
    private func showNewCount(count:Int){
        let showMessageLabel = UILabel(frame: CGRectMake(0, 64 - 44, kUIScreenWidth, 44))
        showMessageLabel.textAlignment = .Center
        showMessageLabel.font = UIFont.systemFontOfSize(16)
        showMessageLabel.backgroundColor = UIColor.yellowColor()
        if count > 0{
            showMessageLabel.text = "\(count)条新微薄"
        }else{
            showMessageLabel.text = "暂无更新"

        }
//        添加到视图中
        self.navigationController?.view.insertSubview(showMessageLabel, belowSubview: (navigationController?.navigationBar)!)
        
//        动画效果实现上下出现或隐藏
        UIView.animateWithDuration(2, animations: { () -> Void in
            showMessageLabel.transform = CGAffineTransformMakeTranslation(0, 44)
            }) { (_) -> Void in
                UIView.animateWithDuration(2, animations: { () -> Void in
                    showMessageLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
//                        销毁或者隐藏
                       showMessageLabel.removeFromSuperview()
                })
        }
        
    }



    
//    加载小菊花
    private lazy var pullView:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
//    懒加载下拉刷新的视图
    private lazy var refreshView:LTRefreshView = LTRefreshView()
    

}

extension LTQHomeViewController{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuslistViewModel!.array.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(LTQHomeViewControllerReuserIdenfiter, forIndexPath: indexPath) as! LTQHomeTableViewCell
        
        cell.statusViewModel = statuslistViewModel?.array[indexPath.row]
        
        
//        由于要实现上提更多进行加载数据，考虑实现滑动到倒数第二个cell时就进行数据的加载，需要进行判断
        if indexPath.row == (statuslistViewModel?.array.count)! - 1 && !pullView.isAnimating(){
            
//         先让小菊花转起来，下一步就是要实现加载更多的数据
            self.pullView.startAnimating()
            
//            加载更多的数据
            loadData()
            
            print("加载新的数据")
            
            
        }
        
        return cell
        
    }
}
